In [2]:
# This notebook creates a matrix for the electricity demand from the from the European Commission's Joint Research Centre’s EU Energy Atlas.
# Each plane of the matrix is dedicated for one demand sector.
# The values are in MWh
In [1]:
import rasterio
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
In [2]:
from IPython.core.display import display, HTML
display(HTML("<style>.output_scroll { height: 500px !important; }</style>"))
/var/folders/dq/9f7l1n4s0w5dyky898r6tbzh0000gn/T/ipykernel_62436/2030294557.py:1: DeprecationWarning: Importing display from IPython.core.display is deprecated since IPython 7.14, please import from IPython display
  from IPython.core.display import display, HTML
In [3]:
file_path_industry = "/Users/kobabendiks/Desktop/UNI/Bachelor Thesis/Datasets/Demand/industry_electricity_demand_2019.tif"  
file_path_others = "/Users/kobabendiks/Desktop/UNI/Bachelor Thesis/Datasets/Demand/othersectors_electricity_demand_2019.tif" 
file_path_transport = "/Users/kobabendiks/Desktop/UNI/Bachelor Thesis/Datasets/Demand/transport_electricity_demand_2019.tif" 
file_path_transform = "/Users/kobabendiks/Desktop/UNI/Bachelor Thesis/Datasets/Demand/transformationinputs_electricity_demand_2019.tif" 
#file_path_nonenergy = "/Users/kobabendiks/Desktop/UNI/Bachelor Thesis/Datasets/Demand/nonenergyuse_electricity_demand_2019.tif"

file_path_total = "/Users/kobabendiks/Desktop/UNI/Bachelor Thesis/Datasets/Demand/total_electricity_demand_2019.tif"  
In [5]:
reference_array = np.load('/Users/kobabendiks/Desktop/UNI/Bachelor Thesis/Datasets/Finalised/Arrays/Reference_array.npy')
In [6]:
#Create matrix for each demand type and convert Toe to MWh

import numpy as np

# Read the TIFF file
def read_tiff(file_path):
    with rasterio.open(file_path) as src:
        data = src.read(1)  # Read the first band (assumes single-band TIFF)
        profile = src.profile  # Metadata profile of the TIFF
    return data, profile

# Create a matrix of values from the TIFF file
def create_matrix(file_path):
    # Read the TIFF file
    data, _ = read_tiff(file_path)  
    return data

matrix_industry = create_matrix(file_path_industry)
matrix_others = create_matrix(file_path_others)
matrix_transport = create_matrix(file_path_transport)
matrix_transform = create_matrix(file_path_transform)
#matrix_nonenergy = create_matrix(file_path_nonenergy)

matrix_total = create_matrix(file_path_total)

#convert arrays from toe to MWh
matrix_industry_MWh = matrix_industry *11.63
matrix_others_MWh = matrix_others *11.63
matrix_transport_MWh = matrix_transport *11.63
matrix_transform_MWh = matrix_transform *11.63
#matrix_nonenergy_MWh = matrix_nonenergy *11.63

matrix_total_MWh = matrix_total *11.63


# Assuming reference_array and matrix_industry_MWh are NumPy arrays of the same shape
matrix_industry_MWh_St = reference_array.copy()
matrix_transport_MWh_St = reference_array.copy()
matrix_transform_MWh_St = reference_array.copy()
matrix_others_MWh_St = reference_array.copy()
#matrix_nonenergy_MWh_St = reference_array.copy()
matrix_total_MWh_St = reference_array.copy()


# Add matrix_industry_MWh values where they are greater than 0
matrix_industry_MWh_St[matrix_industry_MWh > 0] += matrix_industry_MWh[matrix_industry_MWh > 0]
matrix_others_MWh_St[matrix_others_MWh > 0] += matrix_others_MWh[matrix_others_MWh > 0]
matrix_transport_MWh_St[matrix_transport_MWh > 0] += matrix_transport_MWh[matrix_transport_MWh > 0]
matrix_transform_MWh_St[matrix_transform_MWh > 0] += matrix_transform_MWh[matrix_transform_MWh > 0]
#matrix_nonenergy_MWh_St[matrix_nonenergy_MWh > 0] += matrix_nonenergy_MWh[matrix_nonenergy_MWh > 0]
matrix_total_MWh_St[matrix_total_MWh > 0] += matrix_total_MWh[matrix_total_MWh > 0]
In [39]:
#Display Matrices

def display_matrix (matrix):
    colors = [(0, 0, 0)] + [(i / 255, i / 255, i / 255) for i in range(256)]
    cmap = LinearSegmentedColormap.from_list("custom_cmap", colors, N=256)

    # Plot the entire matrix
    plt.figure(figsize=(100, 100))
    plt.imshow(matrix, cmap=cmap, vmin=-1, vmax=1)  # Adjust the vmin and vmax if necessary
    plt.colorbar()  # Add a color scale bar
    plt.axis('off')  # Remove axis for clean display
    plt.tight_layout()
    plt.show()
    
display_matrix(matrix_industry_MWh_St)
display_matrix(matrix_others_MWh_St)
display_matrix(matrix_transport_MWh_St)
display_matrix(matrix_transform_MWh_St)
#display_matrix(matrix_nonenergy_MWh)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [28]:
def stats_matrix(matrix):
    # Print basic statistics from the matrix
    print("Matrix Statistics:")
    print(f"Maximum value: {np.max(matrix)}")
    print(f"Minimum value: {np.min(matrix)}")
    print(f"Mean value: {np.mean(matrix)}")
    print(f"Standard deviation: {np.std(matrix)}", '\n')
    
stats_matrix(matrix_industry_MWh)
stats_matrix(matrix_industry_MWh_St)
stats_matrix(matrix_others_MWh)
stats_matrix(matrix_others_MWh_St)
stats_matrix(matrix_transport_MWh)
stats_matrix(matrix_transport_MWh_St)
stats_matrix(matrix_transform_MWh)
stats_matrix(matrix_transform_MWh_St)
#stats_matrix(matrix_nonenergy_MWh)

stats_matrix(matrix_total_MWh)
stats_matrix(matrix_total_MWh_St)
Matrix Statistics:
Maximum value: 8771240.0
Minimum value: -11.630000114440918
Mean value: 30.218393325805664
Standard deviation: 3525.478759765625 

Matrix Statistics:
Maximum value: 8771240.0
Minimum value: -1.0
Mean value: 36.84597396850586
Standard deviation: 3525.40283203125 

Matrix Statistics:
Maximum value: 6810602.0
Minimum value: -11.630000114440918
Mean value: 51.92584228515625
Standard deviation: 1759.1580810546875 

Matrix Statistics:
Maximum value: 6810602.0
Minimum value: -1.0
Mean value: 58.55318832397461
Standard deviation: 1758.9141845703125 

Matrix Statistics:
Maximum value: 196030.578125
Minimum value: -11.630000114440918
Mean value: -5.124302864074707
Standard deviation: 94.0363998413086 

Matrix Statistics:
Maximum value: 196030.578125
Minimum value: -1.0
Mean value: 1.5032137632369995
Standard deviation: 93.70645141601562 

Matrix Statistics:
Maximum value: 1011761.5625
Minimum value: -11.630000114440918
Mean value: 2.367494583129883
Standard deviation: 336.0987243652344 

Matrix Statistics:
Maximum value: 1011761.5625
Minimum value: -1.0
Mean value: 8.995014190673828
Standard deviation: 335.8580627441406 

Matrix Statistics:
Maximum value: 8771970.0
Minimum value: -11.630000114440918
Mean value: 101.76504516601562
Standard deviation: 4043.225830078125 

Matrix Statistics:
Maximum value: 8771970.0
Minimum value: -1.0
Mean value: 108.39259338378906
Standard deviation: 4043.0498046875 

In [24]:
array = matrix_industry_MWh
y, x = np.unravel_index(np.argmin(array), array.shape)

print(f"Smallest value is at: (y={y}, x={x})")
Smallest value is at: (y=0, x=1701)
In [40]:
#Combine individual matrices into one 3d array (each plane is one type), named matrix_complete
#This axis represents the "stack" of the individual 2D arrays (or "spreadsheets"), with matrix_industry being 0 and matrix_transform being 3

matrix_complete = np.stack((matrix_industry_MWh_St, matrix_others_MWh_St, matrix_transport_MWh_St, matrix_transform_MWh_St), axis=0)
print("Number of planes", len(matrix_complete),'\n',"Number of pixels for y axis:",len(matrix_complete[0]),'\n',"Number of pixels for x axis:",len(matrix_complete[0][0]))
Number of planes 4 
 Number of pixels for y axis: 4478 
 Number of pixels for x axis: 5563
In [26]:
#create matrix of total energy demand form individual sectors
matrix_summed = np.sum(matrix_complete, axis=0)
print("Number of pixels for y axis:",len(matrix_summed),'\n',"Number of pixels for x axis:",len(matrix_summed[0]))
Number of pixels for y axis: 4478 
 Number of pixels for x axis: 5563
In [36]:
#compare calculated total demand to given total demand
comparison_matrix1 = np.stack((matrix_summed, matrix_total_MWh_St), axis=0)
comparison_matrix2 = np.subtract.reduce(comparison_matrix1, axis=0)

def stats_matrix(matrix):
    # Print basic statistics from the matrix
    print("Matrix Statistics:")
    print(f"Maximum value: {np.max(matrix)}")
    print(f"Minimum value: {np.min(matrix)}")
    print(f"Mean value: {np.mean(matrix)}")
    print(f"Standard deviation: {np.std(matrix)}", '\n')
    
stats_matrix(comparison_matrix2)

count = np.sum(comparison_matrix2 > 0)
print("Number of non-matching values:",count)
Matrix Statistics:
Maximum value: 1.0
Minimum value: -3.0
Mean value: -2.4952380657196045
Standard deviation: 1.1222779750823975 

Number of non-matching values: 343051
In [41]:
import numpy as np

# Define the file path
file_path = '/Users/kobabendiks/Desktop/UNI/Bachelor Thesis/Datasets/Finalised Arrays/Demand_array.npy'

# Save an array
np.save(file_path, matrix_complete)
In [ ]: